# 02_political_knowledge_political_attitude.R
# Created: 2020-8-24 Taka-aki Asano
# Last Modified: 2023-2-19

# package
require("ggpubr")
require("ltm")

# t-test
## internal political efficacy
efficacy_mean <- ggbarplot(voter2019, x = "Knowledge_Class", y = "EFFICACY", 
                           label = TRUE, lab.nb.digits = 2, lab.vjust = -2.0, 
                           add = "mean_ci", ylim = c(1, 5)) + 
  stat_compare_means(comparisons = list(c("Class1", "Class2"), 
                                        c("Class1", "Class3"), 
                                        c("Class1", "Class4")), 
                     label = "p.signif", method = "t.test", 
                     label.x = 1.5, label.y = c(3.5, 4, 4.5), 
                     symnum.args = list(cutpoints = c(0, 0.01, 0.05, 0.1, 1), 
                                        symbols = c("***", "**", "*", ""))) + 
  labs(x = "Pattern of Political Knowledge", 
       y = expression(Low %<-% "Internal Political Efficacy" %->% High), 
       title = "(a) Internal Political Efficacy") + 
  theme(plot.title = element_text(hjust = 0.5), 
        axis.text.x = element_text(size = 10))
plot(efficacy_mean)

## political interest
voter2019$INTEREST_RE <- recode(voter2019$INTEREST, `1` = 5, `2` = 4, `3` = 3, `4` = 2, `5` = 1)
interest_mean <- ggbarplot(voter2019, x = "Knowledge_Class", y = "INTEREST_RE", 
                           label = TRUE, lab.nb.digits = 2, lab.vjust = -2.0, 
                           add = "mean_ci", ylim = c(1, 5)) + 
  stat_compare_means(comparisons = list(c("Class1", "Class2"), 
                                        c("Class1", "Class3"), 
                                        c("Class1", "Class4")), 
                     label = "p.signif", method = "t.test", 
                     label.x = 1.5, label.y = c(3.7, 4.2, 4.7), 
                     symnum.args = list(cutpoints = c(0, 0.01, 0.05, 0.1, 1), 
                                        symbols = c("***", "**", "*", ""))) + 
  labs(x = "Pattern of Political Knowledge", 
       y = expression(Low %<-% "Political Interest" %->% High), 
       title = "(b) Political Interest") + 
  theme(plot.title = element_text(hjust = 0.5), 
        axis.text.x = element_text(size = 10))
plot(interest_mean)

## ideology
ideology2019 <- paste("ISSUE_", 1:10, sep = "")
irt2019_ideology <- ltm::grm(voter2019[,ideology2019], IRT.param = FALSE)
irt2019_ideology
score2019 <- ltm::factor.scores(irt2019_ideology, voter2019[,ideology2019])
voter2019$IDEOLOGY <- abs(score2019$score.dat$z1)
ideology_mean <- ggbarplot(voter2019, x = "Knowledge_Class", y = "IDEOLOGY", 
                           label = TRUE, lab.nb.digits = 2, lab.vjust = -2.0, 
                           add = "mean_ci", ylim = c(0.1, 1.5)) + 
  stat_compare_means(comparisons = list(c("Class1", "Class2"), 
                                        c("Class1", "Class3"), 
                                        c("Class1", "Class4")), 
                     label = "p.signif", method = "t.test", 
                     label.x = 1.5, label.y = c(0.9, 1.1, 1.3), 
                     symnum.args = list(cutpoints = c(0, 0.01, 0.05, 0.1, 1), 
                                        symbols = c("***", "**", "*", ""))) + 
  labs(x = "Pattern of Political Knowledge", 
       y = expression(Moderate %<-% "Ideology" %->% Extreme), 
       title = "(c) Ideological Extremity") + 
  theme(plot.title = element_text(hjust = 0.5), 
        axis.text.x = element_text(size = 10))
plot(ideology_mean)
